package org.feng.controller;

import org.feng.client.BaiduAiImageClassifyClient;
import org.feng.client.request.DishesImageClassifyRequest;
import org.feng.client.request.PlantImageClassifyRequest;
import org.feng.client.response.DishesImageClassifyResponse;
import org.feng.client.response.PlantImageClassifyResponse;
import org.feng.vo.PlantImageClassifyVO;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Path;

/**
 * TODO
 *
 * @author feng
 */
@RequestMapping("/image/classify")
@RestController
public class ImageClassifyController {

    @Resource
    private BaiduAiImageClassifyClient client;

    @Resource
    private ResourceLoader resourceLoader;

    @PostMapping("/plant/v2")
    public PlantImageClassifyResponse plantClassify(@RequestParam("image") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return null;
        }

        // 设置token
        client.setAccessToken("24.0570fbd291e611127e3b8f46adb57164.2592000.1717063687.282335-65442625");

        // 获取上传的图片路径
        org.springframework.core.io.Resource fileResource = file.getResource();
        Path imagePath = fileResource.getFile().toPath();

        PlantImageClassifyRequest request = new PlantImageClassifyRequest();
        request.setBaiKeNum(10);
        request.setImage(imagePath);

        return client.recognizeImage(request, PlantImageClassifyResponse.class);
    }


    /**
     * 植物识别
     */
    @PostMapping("/plant")
    public PlantImageClassifyVO plantClassify() throws IOException {
        // 设置token
        client.setAccessToken("24.0570fbd291e611127e3b8f46adb57164.2592000.1717063687.282335-65442625");

        PlantImageClassifyRequest request = new PlantImageClassifyRequest();
        request.setBaiKeNum(10);

        org.springframework.core.io.Resource imageResource = resourceLoader.getResource("classpath:" + "imgs/img1.png");
        request.setImage(imageResource.getFile().toPath());

        PlantImageClassifyResponse classifyResponse = client.recognizeImage(request, PlantImageClassifyResponse.class);
        PlantImageClassifyVO plantImageClassifyVO = PlantImageClassifyVO.newInstance(classifyResponse);
        return plantImageClassifyVO;
    }

    /**
     * 菜品识别
     */
    @PostMapping("/dish")
    public DishesImageClassifyResponse dishClassify() throws IOException {
        // 设置token
        client.setAccessToken("24.0570fbd291e611127e3b8f46adb57164.2592000.1717063687.282335-65442625");

        DishesImageClassifyRequest request = new DishesImageClassifyRequest();
        request.setBaiKeNum(10);
        request.setFilterThreshold(BigDecimal.valueOf(0.95));
        request.setTopNum(5);

        org.springframework.core.io.Resource imageResource = resourceLoader.getResource("classpath:" + "imgs/img1.png");
        request.setImage(imageResource.getFile().toPath());

        DishesImageClassifyResponse classifyResponse = client.recognizeImage(request, DishesImageClassifyResponse.class);
        return classifyResponse;
    }
}
